

import java.sql.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class Building {
    final static int TYPE_DORM = 0;
    final static int TYPE_FORMAL = 1;
    final static int TYPE_THEME = 2;
    
    
    public int buildingID;
    public String buildingName;
    public int type;
    public int managerID;
    public String description;
    
    private Connection connect;

    public Building(String buildingName, int type, int managerID, String description) {
        this.buildingName = buildingName;
        this.type = type;
        this.managerID = managerID;
        this.description = description;
    }
    
  
    
    public Building(int buildingID, Connection connect){
        this.connect = connect;
        this.buildingID = buildingID;
        loadFromDB();
    }
    
    
    //Insert new entry to db
    public boolean addToDB(Connection connect){
        try {
            String addQuery = "INSERT INTO DORMS.building (buildingName, type, managerID, description) "
                    + "VALUES (?,?,?,?)";
            PreparedStatement addStmt = connect.prepareStatement(addQuery);
            addStmt.setString(1, buildingName);
            addStmt.setInt(2, type);
            addStmt.setInt(3, managerID);
            addStmt.setString(4, description);
            addStmt.execute();
            return true;
            
            
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to add to database");
             
        }
        return false;
    }
    
    
    
    public boolean update(){
        try {
            String updateQuery = "UPDATE DORMS.building SET buildingName = ?, type = ?,"
                    + "managerID = ?, description = ? WHERE buildingID = ?";
            PreparedStatement updateStmt = connect.prepareStatement(updateQuery);
            updateStmt.setString(1, buildingName);
            updateStmt.setInt(2, type);
            updateStmt.setInt(3, managerID);
            updateStmt.setString(4, description);
            updateStmt.setInt(5, buildingID);
             updateStmt.executeUpdate();
            updateStmt.executeUpdate();
            return true;
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to update to database");
             
        }
        return false;
    }
    
    
    public boolean delete(){
        try {
            String deleteRoomQuery = "DELETE FROM DORMS.room WHERE buildingID = ?";
            PreparedStatement deleteRoomStmt = connect.prepareStatement(deleteRoomQuery);
            deleteRoomStmt.setInt(1, buildingID);
            deleteRoomStmt.executeUpdate();
            
            
            String deleteQuery = "DELETE FROM DORMS.building WHERE buildingID = ?";
            PreparedStatement updateStmt = connect.prepareStatement(deleteQuery);
            updateStmt.setInt(1, buildingID);
            updateStmt.execute();
            return true;
            
            
        }catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to delete from database");
             
        }
        return false;
        
    }
    


    public ArrayList<Room> getRooms() {
        ArrayList<Room> roomList = new ArrayList<Room>();

        
        String query = "Select * from DORMS.room where buildingID = " + buildingID;
        try {
                Statement stmt = connect.createStatement();
                ResultSet rset = stmt.executeQuery(query);
                while (rset.next())
                {

                  int id = rset.getInt(1);

                    Room room = new Room(id,connect);

                    roomList.add(room);
                }
        } catch(SQLException E) {

        }

        return roomList;

    }
    
    public String toString(){
        return buildingName;
    }
            
      
    
    
    //Load object with info from db
    private void loadFromDB(){
        try {
            String loadQuery = "select * from building where buildingID = ?";
            PreparedStatement loadStmt = connect.prepareStatement(loadQuery);
            loadStmt.setInt(1, buildingID);
            ResultSet roomInfo = loadStmt.executeQuery();
            if(roomInfo.next()){
                buildingName = roomInfo.getString("buildingName");
                type = roomInfo.getInt("type");
                managerID = roomInfo.getInt("managerID");
                description = roomInfo.getString("description");
            }
            
            
            
        } catch (Exception ex){
            JOptionPane.showMessageDialog(null,
                    "Unable to load Building from database.");
            
        }
        
    }
    
}
